const http = require('http');
const fs = require('fs');
const path = require('path');

const server = http.createServer();

server.on('request',(req,res) => {
    console.log('服务器正被访问');
    const url = req.url;
    //优化 让用户访问时直接到首页，或写/index.html到首页
    // const fpath = path.join(__dirname,url);
    let fpath = '';
    if (url === '/') {
        fpath = path.join(__dirname,'/clock/index.html');
    }else {
        fpath = path.join(__dirname,'/clock',url);
    }
    fs.readFile(fpath,'utf8',(err,dataStr) => {
        if (err) {
            return res.end('<h1>404 Not found</h1>');
        }
        // res.setHeader('Content-Type','text/html; charset=utf-8');
        res.end(dataStr);
    })
})

server.listen(80,() => {
    console.log('set http://127.0.0.1:80');
})

// /clock/index.html